//
//  WZAuthViewController.swift
//  weibo
//
//  Created by mac on 16/2/26.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class WZAuthViewController: UIViewController,UIWebViewDelegate {

    var webView: UIWebView?
    
   
    
    override func loadView() {
        //        let webView = UIWebView(frame: UIScreen.mainScreen().bounds);
        let webView = UIWebView();
        self.webView = webView
        
        view = webView
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        setNavBarItem()
        self.webView?.delegate = self
        print(self.view)
        loadWebView()

    }
    
    
    func loadWebView(){
//        https://api.weibo.com/oauth2/authorize?client_id=123050457758183&redirect_uri=http://www.example.com/response&response_type=code
        let url = NSURL(string:"https://api.weibo.com/oauth2/authorize?client_id=517511500&redirect_uri=http://www.baidu.com&response_type=code")
      
        let request = NSURLRequest(URL: url!)
        webView?.loadRequest(request)
        
    }
   
    func setNavBarItem(){
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: UIBarButtonItemStyle.Plain, target: self, action: "autoClick");
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "dismissClick");
    }
    
//  MARK:－－设置上面两个按钮的点击方法
    func autoClick(){
        let js = "document.getElementById(\"userId\").value = \"1129830296@qq.com\";document.getElementById(\"passwd\").value = \"wen15233217989\";"
        
        webView?.stringByEvaluatingJavaScriptFromString(js)
    }
    func dismissClick(){
        dismissViewControllerAnimated(true) { () -> Void in
            
        }
    }
    
    
    
    }
extension WZAuthViewController{
    
    func webViewDidStartLoad(webView: UIWebView) {
        print("开始加载")
    }
    func webViewDidFinishLoad(webView: UIWebView) {
         print("加载完成")
    }
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
         print("加载出错")
    }
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

        /* // 第一种方法
        if let urlStr = request.URL?.absoluteString {
            print(urlStr)
           www.baidu.com/?code=fbddba5571633ef60db9b63aa5e5c331)
            if urlStr.hasPrefix("http://www.baidu.com"){
            let  code = urlStr.componentsSeparatedByString("=").last
        print(code)
        }
        }
        */

        
               if let urlStr = request.URL?.absoluteString{
            
            
            if urlStr.hasPrefix("http://www.baidu.com"){
                if let myCode = request.URL?.query{
                    
                    let code = myCode.substringFromIndex("code=".endIndex)
                    
                    print(code)
//                    let userViewModel = WZUserViewModel()
                    WZUserViewModel.shareInstance.getAccessToken(code, success: { () -> () in
                        print("登录成功")
                        
                        //      MARK:创建通知 用来通知appDelegate更换根控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(KchangeRootVC, object: nil)
                        
                        self.dismissViewControllerAnimated(false, completion: { () -> Void in
                           
                        })
                      
                        
                        }, faile: { () -> () in
                            print("登录")
                    })
                    //解决出现闪百度的问题
                    return false
                }
 
                
            }
            
        }
        

        return true
    }
    
}


